﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class webadmin_pages_backup_restore_database : System.Web.UI.Page
{
    public string messageError = "";
    public string sfilePath = "";
    Common comm = new Common();
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string mess = Page.MapPath("");
            mess = mess.Remove(mess.LastIndexOf('\\'));
            mess = mess.Remove(mess.LastIndexOf('\\'));
            sfilePath=@"" + mess + "\\Uploads\\Backup\\";
            LoadDanhMucGoc("");
        }
        catch (Exception ex)
        {
            messageError = "Lỗi hệ thống - " + ex.Message;
        }
    }
    protected void LoadDanhMucGoc(string id)
    {
        string mess = Page.MapPath("");
        mess = mess.Remove(mess.LastIndexOf('\\'));
        mess = mess.Remove(mess.LastIndexOf('\\'));
        cboFileBackup.Items.Clear();
        ListItem li;
        string[] duongdan;
        string[] filePaths = Directory.GetFiles(sfilePath);
        for (int i = 0; i < filePaths.Length; i++)
        {
            mess += "<br/>" + filePaths[i].ToString();
            duongdan = filePaths[i].ToString().Split('\\');
            li = new ListItem(duongdan[duongdan.Length - 1]);
            cboFileBackup.Items.Add(li);
        }
    }
    private void RestoreDb()
    {
        string sql="RESTORE DATABASE lephuoc_vlbinhduong FROM DISK = '"+sfilePath+cboFileBackup.SelectedItem.Text+".bak'"+
            " WITH FILE = 6 NORECOVERY;";
        comm.execNoneQuery(sql);
        txtTen.Text = "";
        LoadDanhMucGoc("");
    }

    protected void btnBackup_Click(object sender, EventArgs e)
    {
        string sql = "BACKUP DATABASE lephuoc_vlbinhduong TO DISK='"+sfilePath+txtTen.Text.Trim()+".bak'";
        comm.execNoneQuery(sql);
        txtTen.Text = "";
        LoadDanhMucGoc("");
    }
}
